﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form" lay-filter="adminform">
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">任务组名</label>
                    <div class="layui-input-block">
                        <input type="text" id="JobGroup" name="JobGroup" maxlength="50" lay-verify="required" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">任务名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="JobName" name="JobName" maxlength="50" value="" lay-verify="required" class="layui-input ">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">任务类型</label>
                    <div class="layui-input-block" id="JobType">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">文件名称</label>
                    <div class="layui-input-block">
                        <select id="FileName" name="FileName" lay-verify="required">
                            <option value="">==请选择==</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">请求地址</label>
                    <div class="layui-input-block">
                        <input type="text" id="RequestUrl" name="RequestUrl" value="" lay-verify="required" class="layui-input ">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">请求头</label>
                    <div class="layui-input-block">
                        <input type="text" id="RequestHeaders" name="RequestHeaders" class="layui-input ">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">请求参数</label>
                    <div class="layui-input-block">
                        <input type="text" id="RequestString" name="RequestString" class="layui-input ">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label required">Cron表达式</label>
                    <div class="layui-input-block">
                        <input type="text" id="CronExpress" name="CronExpress" maxlength="50" lay-verify="required" value="" class="layui-input ">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">选项</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="DoItNow" id="DoItNow" checked="" value="true" title="立即执行">
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">是否记录日志</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="IsLog" id="IsLog" checked="" value="是" title="是">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text layui-hide">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea id="Description" name="Description" class="layui-textarea" placeholder="请输入备注"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    layui.use(['jquery', 'form', 'laydate', 'common', 'optimizeSelectOption', 'cron'], function () {
        var form = layui.form,
            $ = layui.$,
            common = layui.common,
            cron = layui.cron,
            laydate = layui.laydate;
        var keyValue = $.request("keyValue");
        //权限字段
        common.authorizeFields('adminform');
        $(function () {
            initControl();
            if (!!keyValue) {
                common.ajax({
                    url: "/SystemSecurity/OpenJobs/GetFormJson",
                    dataType: "json",
                    data: { keyValue: keyValue },
                    async: false,
                    success: function (data) {
                        common.val('adminform', data);
                        if(data.IsLog=='是')
                        {
                            $('#IsLog').attr("checked", 'checked');
                        }
                        changeJobType(data.JobType);
                        cron.render({
                            elem: "#CronExpress",           // 绑定元素
                            value: data.CronExpress, // 默认值
                            done: function (cronStr) {  // 点击确定，或运行时，触发，参数为 cron 表达式字符串
                                $("#CronExpress").val(cronStr);
                            },
                        });
                    }
                });
            }
            else {
                changeJobType(0);
                cron.render({
                    elem: "#CronExpress",           // 绑定元素
                    value: "0/20 * * * * ?", // 默认值
                    done: function (cronStr) {  // 点击确定，或运行时，触发，参数为 cron 表达式字符串
                        $("#CronExpress").val(cronStr);
                    },
                });
            }
            form.render();
        });
        wcLoading.close();
        function initControl() {
            $("#FileName").bindSelect({
                url: "/SystemSecurity/OpenJobs/QueryLocalHandlers",
                id: "Key",
                text:"Description"
            });
            $("#JobType").bindRadio({
                data: top.clients.dataItems["JobType"],
                id: ""
            });
        }
        form.on('radio(JobType)', function (data) {
            changeJobType(data.value);
        });
        function changeJobType(value) {
            if (value == "0") {
                $('#FileName').parent().parent().removeClass('layui-hide');
                $('#FileName').attr('lay-verify', 'required');
                $('#RequestUrl').parent().parent().addClass('layui-hide');
                $('#RequestUrl').removeAttr('lay-verify', 'required');
                $('#RequestHeaders').parent().parent().addClass('layui-hide');
                $('#RequestString').parent().parent().addClass('layui-hide');
                $('#RequestUrl').val("");
                $('#RequestHeaders').val("");
                $('#RequestString').val("");
            }
            else {
                $('#FileName').parent().parent().addClass('layui-hide');
                $('#FileName').removeAttr('lay-verify');
                $('#RequestUrl').parent().parent().removeClass('layui-hide');
                $('#RequestUrl').attr('lay-verify', 'required');
                $('#RequestHeaders').parent().parent().removeClass('layui-hide');
                $('#RequestString').parent().parent().removeClass('layui-hide');
                $('#FileName').val("");
            }
            form.render();
        }
        //select验证
        form.verify({
            required: function (value, item) {
                var msg = "必填项不能为空";
                value = $.trim(value);
                var isEmpty = !value || value.length < 1;
                // 当前验证元素是select且为空时,将页面定位至layui渲染的select处，或自定义想定位的位置
                if (item.tagName == 'SELECT' && isEmpty) {
                    $("html").animate({
                        scrollTop: $(item).siblings(".layui-form-select").offset().top - 74
                    }, 50);
                }
                if (isEmpty) {
                    return msg;
                }
            }
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var postData = data.field;
            if (!postData["EnabledMark"]) postData["EnabledMark"] = false;
            if (!postData["IsLog"]) postData["IsLog"] = "否";
            common.submitForm({
                url: "/SystemSecurity/OpenJobs/SubmitForm?keyValue=" + keyValue,
                param: postData,
                success: function () {
                    common.parentreload("data-search-btn");
                }
            })
            return false;
        });
    });
</script>